import { pathToBase64, base64ToPath } from '../../js_sdk/mmmm-image-tools/index.js'
// 获取storage
// export const getStorageData = (params) => {
// 	return new Promise((resolve, reject) => {
// 		uni.getStorage({
// 			key: params,
// 			success(res) {
// 				resolve(res.data)
// 			},
// 			fail(res) {
// 				resolve('')
// 			}
// 		})
// 	})
// };

// 获取storage
// getStorageSyncData('userId')
export const getStorageSyncData = (name) => {
	return uni.getStorageSync(name)
}

// 存储storage
// setStorageData('userId', res)
export const setStorageData = (params, data) => {
	uni.setStorage({
		key: params,
		data: data,
	})
};

// 图片转base64
export const imgToBase64 = (path) => {
	return new Promise((resolve, reject) => {
		// #ifdef MP-WEIXIN
		uni.getFileSystemManager().readFile({
			filePath: path, //选择图片返回的相对路径
			encoding: 'base64', //编码格式
			success: res => { //成功的回调
				let base64 = 'data:image/jpeg;base64,' + res.data //不加上这串字符，在页面无法显示的哦
				resolve(base64)
			},
			fail: (e) => {
				console.log("图片转换失败");
			}
		})
		// #endif
		// #ifndef MP-WEIXIN
		pathToBase64(path).then(base64 => {
				resolve(base64)
		  })
		  .catch(error => {
		    console.error(error)
		  })
		// #endif
	})
}

export const bankReplace = (value) => {
	let b = '';
	if (value && value.length > 8) {
		b = `${value.substring(0, 4).replace(/\d/g, "*")} ${"*".repeat(value.length - 8).replace(/(.{4})/g, `$1 `)}${value.length % 4 ? " " : ""}${value.slice(-4)}`
	} else {
		b = value;
	}
	let v = ''
	if (b) {
		v = b.split(' ')
	} else{
		v = ''
	}
	return v
}

// 判断开始结束时间大小 value1开始 value2结束
export const timeJudge = (value1,value2) => {
    var startDate = new Date(value1);
    var endDate = new Date(value2);
    if (startDate.getTime() == endDate.getTime()) {
		// 开始时间等于结束时间
        return '1';
    } else if(startDate.getTime() > endDate.getTime()){
		// 开始时间大于结束时间
		return '2';
	} else {
		// 结束时间大于开始时间
        return '3';
    }
}

// 手机号正则效验
export const isPoneAvailable = (val) => {
	var myreg=/^[1][3,4,5,6,7,8,9][0-9]{9}$/;
	if (!myreg.test(val)) {
		// 错误格式
		return '1';
	} else {
		// 正确格式
		return '2';
	}
}

/**
 * 计算距离 Number(radius):半径范围 this.latitude:当前纬度  signData.hosLatitude 坐标点纬度
 */
export const getDistance = (latitude, hosLatitude, longitude, hosLongitude, radius) => {
	var radLat1 = Rad(Number(latitude))
	var radLat2 = Rad(Number(hosLatitude))
	var a = radLat1 - radLat2;
	var b = Rad(longitude) - Rad(Number(hosLongitude));
	var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
		Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)))
	s = s * 6378.137
	s = Math.round(s * 10000) / 10000
	s = s.toFixed(2) //保留两位小数
	if (s * 1000 < Number(Number(radius) + 100)) {
		console.log("未超出范围：" + s)
		return true
	} else {
		console.log("超出范围：" + s);
		return false
	}
};